'''
Created on 23/09/2010

Data models specification

@author: fccoelho
'''
import datetime
from google.appengine.ext import db
from google.appengine.api import users

class Member(db.Model):
    user = db.UserProperty(required=True)
    member_since = db.DateProperty(auto_now_add=True)
    location = db.GeoPtProperty()



class Model(db.Model):
    name = db.StringProperty(required=True)
    author = db.ReferenceProperty(Member,required = True, verbose_name="My models", collection_name="my_models")
    source = db.TextProperty(required=True) #text is the source xml
    mjson = db.TextProperty(required=True) #model definition in json format
    latex = db.TextProperty(required=True) #model's equations in jasonified latex format
    last_updated = db.DateProperty(auto_now=True)
    public = db.BooleanProperty(required=True)
    shared_with = db.ListProperty(db.Key)
    rating = db.RatingProperty()
    
Member.shared_with_me = property(lambda self: Model.gql("WHERE shared_with = :1", self.key()))

class OpenInvitation(db.Model):
    email = db.EmailProperty(required=True)
    date = db.DateProperty(auto_now_add=True)
    model = db.ReferenceProperty(Model,required=True)
## Stats related tables

class Session(db.Model):
    '''
    Statistics about use sessions
    '''
    date = db.DateProperty(auto_now_add=True)
    member = db.ReferenceProperty(Member, required=True, collection_name="sessions")
    duration = db.IntegerProperty(required=True)
    model = db.ReferenceProperty(Model, collection_name="sessions")
    
class Install(db.Model):
    '''
    Data about unique installs
    '''
    date = db.DateProperty(auto_now_add=True)
    version = db.StringProperty(required=True)
    opsys = db.StringProperty(required=True,verbose_name='Operating-System')